function fileDialogComplete(numFilesSelected, numFilesQueued) {
	try {
		if (numFilesQueued > 0) {
			document.getElementById('btnCancel').disabled = "";
			// this.startUpload();
		}
	} catch (ex) {
		alert(ex);
	}
}

function uploadProgress(file, bytesLoaded) {
	try {
		var percent = Math.ceil((bytesLoaded / file.size) * 100);
		var progress = new FileProgress(file, this.customSettings.upload_target);
		progress.setProgress(percent);
		if (percent === 100) {
			progress.setStatus("<font color='red' style='font: 12px/1.4em Helvetica, Arial, sans-serif'>正在保存文件，请您稍候......</font>");// 正在创建缩略图...
			progress.toggleCancel(false, this);
		} else {
			progress
					.setStatus("<font color='red' style='font: 12px/1.4em Helvetica, Arial, sans-serif'>正在上传......</font>");
			progress.toggleCancel(true, this);
		}
	} catch (ex) {
		alert(ex);
	}
}

function uploadSuccess(file, serverData) {
	try {
		var progress = new FileProgress(file, this.customSettings.upload_target);
		addFileInfo(file.id, "文件上传完成");
	} catch (ex) {
		alert(ex);
	}
}

function addFileInfo(fileId, message) {
	$("#"+fileId).children().eq(3).html("<font color='green'>" + message + "</font>");
	//row.cells[3].innerHTML = "<font color='green'>" + message + "</font>";
}

function cancelUpload() {
	var infoTable = document.getElementById("infoTable");
	var rows = infoTable.rows;
	var ids = new Array();
	var row;
	if (rows == null) {
		return false;
	}
	for (var i = 0; i < rows.length; i++) {
		ids[i] = rows[i].id;
	}
	for (var i = 0; i < ids.length; i++) {
		deleteFile(ids[i]);
	}
}

function fileQueued(file) {
	if($("#tbodyFile").children().length < 12) {
		addReadyFileInfo(file);
	}else{
		swfu.cancelUpload(file.id, false);
	}
}

function addReadyFileInfo(file) {
	if (file == null) {
		return false;
	}
	var appendStr = "<tr id='"+file.id+"'>";
	appendStr += "<td style='height:26px;border-bottom:1px #f0f0f0 dotted;text-overflow:ellipsis;white-space:nowrap;overflow:hidden;'>"+file.name+"</td>";
	appendStr += "<td style='width:60px;border-bottom:1px #f0f0f0 dotted' align='center'>"+getFileSize(file.size, 2)+"</td>";
	appendStr += "<td style='width:60px;border-bottom:1px #f0f0f0 dotted' align='center'>";
	appendStr += "<img src='../../../images/x.png' title='删除' onmouseover='this.style.cursor=\"pointer\"' onclick='deleteFile(this,\""+file.id+"\")'/>";
	appendStr += "</td>";
	appendStr += "<td style='width:100px;border-bottom:1px #f0f0f0 dotted' align='center'>&nbsp;</td>";
	appendStr += "</tr>";
	$("#tbodyFile").append(appendStr);
	initGrid();
}

function deleteFile(obj,fileId) {
	if (fileId == null || fileId == "") {
		return false;
	}
	$(obj).parent().parent().remove();
	swfu.cancelUpload(fileId, false);
}

function uploadComplete(file) {
	try {
		if (this.getStats().files_queued > 0) {
			this.startUpload();
		} else {
			var progress = new FileProgress(file,
					this.customSettings.upload_target);
			progress.setComplete();
			progress.setStatus("<font color='green' style='font: 12px/1.4em Helvetica, Arial, sans-serif'>所有文件上传完毕!</b></font>");
			progress.toggleCancel(false);
		}
	} catch (ex) {
		alert(ex);
	}
}

function uploadError(file, errorCode, message) {
	var imageName = "<font color='red'>文件上传出错!</font>";
	var progress;
	try {
		switch (errorCode) {
			case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED :
				try {
					progress = new FileProgress(file,
							this.customSettings.upload_target);
					progress.setCancelled();
					progress.setStatus("<font color='red'>取消上传!</font>");
					progress.toggleCancel(false);
				} catch (ex) {
					alert(ex);
				}
				break;
			case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED :
				try {
					progress = new FileProgress(file,
							this.customSettings.upload_target);
					progress.setCancelled();
					progress.setStatus("<font color='red'>停止上传!</font>");
					progress.toggleCancel(true);
				} catch (ex2) {
					this.debug(ex2);
				}
			case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED :
				imageName = "<font color='red'>文件太大!</font>";
				break;
			default :
				//alert(message);
				break;
		}
		addFileInfo(file.id, imageName);

	} catch (ex3) {
		alert(ex);
	}

}

function addImage(src) {
	var newImg = document.createElement("img");
	newImg.style.margin = "5px";

	document.getElementById("thumbnails").appendChild(newImg);
	if (newImg.filters) {
		try {
			newImg.filters.item("DXImageTransform.Microsoft.Alpha").opacity = 0;
		} catch (e) {
			newImg.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='
					+ 0 + ')';
		}
	} else {
		newImg.style.opacity = 0;
	}

	newImg.onload = function() {
		fadeIn(newImg, 0);
	};
	newImg.src = src;
}

function fadeIn(element, opacity) {
	var reduceOpacityBy = 5;
	var rate = 30;

	if (opacity < 100) {
		opacity += reduceOpacityBy;
		if (opacity > 100) {
			opacity = 100;
		}

		if (element.filters) {
			try {
				element.filters.item("DXImageTransform.Microsoft.Alpha").opacity = opacity;
			} catch (e) {
				element.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='
						+ opacity + ')';
			}
		} else {
			element.style.opacity = opacity / 100;
		}
	}

	if (opacity < 100) {
		setTimeout(function() {
					fadeIn(element, opacity);
				}, rate);
	}
}

function FileProgress(file, targetID) {
	this.fileProgressID = "divFileProgress";
	this.fileProgressWrapper = document.getElementById(this.fileProgressID);
	if (!this.fileProgressWrapper) {
		this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = this.fileProgressID;

		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer";

		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));

		var progressText = document.createElement("div");
		progressText.className = "progressName";
		progressText.appendChild(document.createTextNode("上传文件: " + file.name
				+ " (" + getFileSize(file.size, 2) + ")"));

		var progressBar = document.createElement("div");
		progressBar.className = "progressBarInProgress";

		var progressStatus = document.createElement("div");
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";

		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressText);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBar);

		this.fileProgressWrapper.appendChild(this.fileProgressElement);
		document.getElementById(targetID).style.height = "33px";
		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
		fadeIn(this.fileProgressWrapper, 0);

	} else {
		this.fileProgressElement = this.fileProgressWrapper.firstChild;
		this.fileProgressElement.childNodes[1].firstChild.nodeValue = "上传文件: "
				+ file.name + " (" + getFileSize(file.size, 2) + ")";
	}

	this.height = this.fileProgressWrapper.offsetHeight;

}
FileProgress.prototype.setProgress = function(percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].style.width = percentage + "%";
};
FileProgress.prototype.setComplete = function() {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setError = function() {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";

};
FileProgress.prototype.setCancelled = function() {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].className = "progressBarError";
	this.fileProgressElement.childNodes[3].style.width = "";
};
FileProgress.prototype.setStatus = function(status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};

FileProgress.prototype.toggleCancel = function(show, swfuploadInstance) {
	this.fileProgressElement.childNodes[0].style.visibility = show
			? "visible"
			: "hidden";
	if (swfuploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[0].onclick = function() {
			swfuploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};

function getFileSize(filesize, how) {
	var unit = "MB";
	var size = filesize / 1024 / 1024
	if (size < 0.01) {
		size = filesize / 1024;
		unit = "KB";
	}
	if (size < 0.0001) {
		size = filesize;
		unit = "Byte";
	}

	size = Math.round(size * Math.pow(10, how)) / Math.pow(10, how);
	return size + unit;

}
